package com.example.weather_news.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.android.volley.Response;
import com.example.weather_news.R;
import com.example.weather_news.db.CityInfo;
import com.example.weather_news.http.HttpRequest;
import com.example.weather_news.util.ShareUtils;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends AppCompatActivity {

    private LinearLayout linear_oval;
    private Button btn_guide;
    private ViewPager vp_guide;

    private List<ImageView> lists;
    private int[] imgs = {R.mipmap.guide_1, R.mipmap.guide_2, R.mipmap.guide_3};

    private int mIndex = 0;//小圆点当前位置

    private int a = 0;

    private ImageView oval;//原点

//    private Handler handler=new Handler(){
//        @Override
//        public void handleMessage(Message msg) {
//            queryCity();
//        }
//    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        boolean b = ShareUtils.getBoolean(GuideActivity.this, ShareUtils.isFirstIn, false);
        if (b) {
            ShareUtils.setBoolean(GuideActivity.this, ShareUtils.FLASH, false);
            startActivity(new Intent(GuideActivity.this, FlashActivity.class));
            finish();
            return;
        }

        setContentView(R.layout.activity_guide);

//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                handler.sendEmptyMessage(1);
//            }
//        }).start();

        if (Build.VERSION.SDK_INT >= 21) {
            View view = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            view.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }

        ShareUtils.setBoolean(GuideActivity.this, ShareUtils.isFirstIn, true);
        initView();
        lists = new ArrayList<ImageView>();
        for (int i = 0; i < imgs.length; i++) {
            ImageView iv = new ImageView(GuideActivity.this);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setImageResource(imgs[i]);
            lists.add(iv);

            oval = new ImageView(GuideActivity.this);
            if (i == mIndex) {
                oval.setImageResource(R.drawable.oval_checked);
            } else {
                oval.setImageResource(R.drawable.oval_unchecked);
            }
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20, 20);
            if (i > 0) {
                params.leftMargin = 30;
            }
            oval.setLayoutParams(params);
            linear_oval.addView(oval);
        }

        vp_guide.setAdapter(new MyViewPager(lists));
        vp_guide.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int
                    positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mIndex = position;
                for (int i = 0; i < linear_oval.getChildCount(); i++) {// 返回的是现实层面上所包含的子View个数
                    ImageView img = (ImageView) linear_oval.getChildAt(i);
                    if (i == position) {
                        img.setImageResource(R.drawable.oval_checked);
                    } else {
                        img.setImageResource(R.drawable.oval_unchecked);
                    }
                }

                if (position == 2) {
                    queryCity();
                    btn_guide.setVisibility(View.VISIBLE);
                    btn_guide.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            startActivity(new Intent(GuideActivity.this, MainActivity.class));
                            finish();
                        }
                    });
                } else {
                    btn_guide.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

//        queryCity();

    }

    private void queryCity() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpRequest.requestCity(new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        try {
                            if (jsonObject.getInt("status") != 0) {
                                return;
                            }
                            JSONArray array = new JSONArray(jsonObject.getString("result"));
                            Log.e("array1", array.length() + "");
                            Gson gson = new Gson();
                            CityInfo info;
                            for (int i = 0; i < array.length(); i++) {
                                info = gson.fromJson(array.getJSONObject(i).toString(), CityInfo
                                        .class);
                                info.save();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, null);
            }
        }).start();

    }

    class MyViewPager extends PagerAdapter {
        private List<ImageView> mList;

        public MyViewPager(List<ImageView> lists) {
            this.mList = lists;
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView view = mList.get(position);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mList.get(position));
        }
    }

    private void initView() {
        vp_guide = (ViewPager) findViewById(R.id.vp_guide);
        linear_oval = (LinearLayout) findViewById(R.id.linear_oval);
        btn_guide = (Button) findViewById(R.id.btn_guide);
    }


}
